package cn.gqr.service.impl;

import cn.gqr.entity.Book;
import cn.gqr.entity.Largefile;
import cn.gqr.mapper.BookMapper;
import cn.gqr.mapper.PictureMapper;
import cn.gqr.service.BookService;
import cn.gqr.tool.Toolkit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private PictureMapper pictureMapper;


    //分页查询全部书本信息
    //根据typeId查询,如果为null则无条件查询
    //key(放数据库字段名称销量,点赞,加入购物车) null不需要排序，不等于null 对key值字段排序
    public Map selectBookByPage(int start, int num, Integer typeId,String key) {
        Map map = new HashMap();
        map.put("books",bookMapper.selectBookByPage(start,num,typeId,key));
        map.put("total",bookMapper.selectBookByPageTotal(typeId));
        return map;
    }

    //根据书本名称的模糊查询并且分页
    public Map selectBookByNamePage(int start, int num, String name) {

        IPage<Book> page = new Page(start,num);

        LambdaQueryWrapper<Book> qw = null;

        if(name!=null && !"".equals(name)){

            qw = new QueryWrapper<Book>().lambda().like(Book::getName,name);

        }

        bookMapper.selectPage(page,qw);

        Map map = new HashMap();

        List<Book> records = page.getRecords();

        String str ="<span style='color:red;'>"+name+"</span>";

        for (Book b : records) {
            //判断这条数据那个字段包含了key这个字符串;
            if(b.getPublisher()!=null && b.getPublisher().matches(".*"+name+".*")){
                b.setPublisher(b.getPublisher().replace(name,str));
            }
            if(b.getName()!=null&&b.getName().matches(".*"+name+".*")){
                b.setName(b.getName().replace(name,str));
            }
            if(b.getAuthor()!=null&&b.getAuthor().matches(".*"+name+".*")){
                b.setAuthor(b.getAuthor().replace(name,str));
            }
        }

        map.put("books",records);
        map.put("total",page.getTotal());

        return map;
    }


    //获取点赞或者销量或者加入购物车前五的书本对象
    public List<Book> selectData5(String key) {
        return bookMapper.selectData5(key);
    }

    //根据id获取图片对象
    public Largefile showPictureById(String id) {
        return pictureMapper.selectById(id);
    }

    //书本添加
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertBook(Book book,MultipartFile file) {
        //防止前台传入id
        book.setId(null);

        if(file!=null){

            Largefile largefile = null;
            //添加图片
            try {

                largefile = Toolkit.filesUpload(new Largefile(), file);

            } catch (IOException e) {

                throw new RuntimeException(e);
            }

            pictureMapper.insert(largefile);

            //设置书本图片id
            book.setPicture(largefile.getId());

        }

        //添加书本
        int i = bookMapper.insert(book);

        return i>0;
    }

    //书本删除
    @Override
    public boolean delBookById(Integer id) {
        int i = bookMapper.deleteById(id);
        return i>0;
    }

    //书本修改
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBook(Book book, MultipartFile file) {

        if(file!=null){

            Largefile largefile = null;
            try {

                largefile = Toolkit.filesUpload(new Largefile(), file);

            } catch (IOException e) {

                throw new RuntimeException(e);
            }

            //添加图片
            pictureMapper.insert(largefile);

            //判断书本图片是空还是有数据
            String imgId = book.getPicture();
            //null 不用执行删除图片的操作
            if(imgId != null && !"".equals(imgId)){

                pictureMapper.deleteById(imgId);

            }
            //设置书本图片id
            book.setPicture(largefile.getId());
        }

        int i = bookMapper.updateById(book);

        return i>0;
    }
}
